package com.hui.dao.impl;

import com.hui.bean.ClassBean;
import com.hui.bean.RoomBean;
import com.hui.bean.StudentBean;
import com.hui.dao.IRoomDao;
import com.hui.dao.IStudentDao;
import com.hui.util.DBUtils;
import org.apache.ibatis.session.SqlSession;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 学生dao层实现类
 * 和数据库打交道的地方
 *
 */
public class StudentDaoImpl implements IStudentDao {
    /**
     * 得到所有的学生
     * @return 返回一个装有所有学生的list集合
     * @throws SQLException
     */
    @Override
    public List<StudentBean> getAllStudent() throws SQLException {

        SqlSession session = DBUtils.getSqlSession();
        IStudentDao mapper = session.getMapper(IStudentDao.class);
        List<StudentBean> studentBeanList = mapper.getAllStudent();
        session.close();
        return studentBeanList;
        //加载驱动
        //获取连接
       }

    /**
     * 通过班级的id得到所有的学生对象集合
     * @param classId  班级的id
     * @return  对应的学生对象集合
     * @throws SQLException
     */
    @Override
    public List<StudentBean> getStudentByClassId(int classId) throws SQLException {
        SqlSession session = DBUtils.getSqlSession();
        IStudentDao mapper = session.getMapper(IStudentDao.class);
        List<StudentBean> studentBeanList = mapper.getStudentByClassId(classId);
        session.close();
        return studentBeanList;
     }

    @Override
    public List<StudentBean> getStudentByRoomId(int roomId) {
        SqlSession session = DBUtils.getSqlSession();
        IStudentDao mapper = session.getMapper(IStudentDao.class);
        List<StudentBean> studentBeanList = mapper.getStudentByRoomId(roomId);
        session.close();
        return studentBeanList;
    }

    /**
     * 添加一个学生
     * 1.添加一个学生
     * 2.通过房间地址查找到对应房间的id
     * 3.通过房间id值执行房间人数加1
     * @param studentBean 学生数据实体
     * @return
     * @throws SQLException
     */
    @Override
    public int saveStudent(StudentBean studentBean) throws SQLException {
        SqlSession session = DBUtils.getSqlSession();
        IStudentDao mapper = session.getMapper(IStudentDao.class);
        int result = mapper.saveStudent(studentBean);
        session.commit();
        session.close();
        return  result;
     }


    /**
     *  通过学生的id删除一个学生
     *  1.删除掉一个学生
     *  2.得到该学生对应的房间id
     *  3.房间减少一个人
     * @param studentId 学生id
     * @return
     * @throws SQLException
     */
    @Override
    public int deleteStudentByStudentId(int studentId) throws SQLException {
        SqlSession session = DBUtils.getSqlSession();
        IStudentDao studentDao = session.getMapper(IStudentDao.class);
        StudentBean student = studentDao.getStudentByStudentId(studentId);
        int result = studentDao.deleteStudentByStudentId(studentId);
        IRoomDao roomDao = session.getMapper(IRoomDao.class);
        roomDao.removeStudentFromRoomByRoomId(student.getMyRoom().getRoomId());
        session.commit();
        session.close();
        return result;
       }

    /**
     * 通过学生的id得到对应的学生
     * @param studentId  学生id
     * @return 返回一个对应的学生对象
     * @throws SQLException
     */
    @Override
    public StudentBean getStudentByStudentId(int studentId) throws SQLException {
        SqlSession session = DBUtils.getSqlSession();
        IStudentDao mapper = session.getMapper(IStudentDao.class);
        StudentBean student = mapper.getStudentByStudentId(studentId);
        session.close();
        return  student;
       }

    /**
     * 学生换房操作
     * 1.更新学生表，传入新的房间id
     * 2.旧 的房间减少一个学生
     * 3.新的房间增加一个学生
     * @param studentId 学生id
     * @param oldRoomId 旧的房间id
     * @param newRoomId 新的房间id
     * @return 受影响行数
     * @throws SQLException
     */
    @Override
    public int updateStudentRoom(int studentId,int oldRoomId, int newRoomId) throws SQLException {
        SqlSession session = DBUtils.getSqlSession();
        IStudentDao studentDao = session.getMapper(IStudentDao.class);
        int result = studentDao.updateStudentRoom(studentId, oldRoomId, newRoomId);

        IRoomDao roomDao = session.getMapper(IRoomDao.class);
        roomDao.removeStudentFromRoomByRoomId(oldRoomId);
        roomDao.addStudentInRoomByRoomId(newRoomId);
        session.commit();
        session.close();
        return result;
    }

    /**
     * 模糊查询
     * 得到对应条件的部分学生
     * @param studentName  学生姓名
     * @param className 班级名
     * @param roomAddress 寝室地址
     * @return  返回符合条件的学生list集合
     * @throws SQLException
     */
    @Override
    public List<StudentBean> getStudentByCondition(String studentName, String className, String roomAddress) throws SQLException {

        SqlSession session = DBUtils.getSqlSession();
        IStudentDao mapper = session.getMapper(IStudentDao.class);
        List<StudentBean> studentBeanList = mapper.getStudentByCondition(studentName, className, roomAddress);
        session.close();
        return studentBeanList;
  }

}
